#!/bin/bash
#
#
#



FILE_NAME=$1
STR=`sed -n '1p' ./${FILE_NAME}`
TEMP_STR=($STR)
BLOCKCHAIN_NAME=${TEMP_STR[1]}
CHANNEL_NAME=$2
STR=`sed -n '4p' ./${FILE_NAME}`
TEMP_STR=($STR)
ORDERER_COUNT=${TEMP_STR[1]}

COUNT_PEER_ADDCHANELL=`expr $# - 2`
STR=$@
ARG_ARR=($STR)
function peerJoinChannel(){
	  line=`sed -n "/peers:  #${CHANNEL_NAME}/=" ${FILE_NAME} | tail -n1`
  	#line=`expr ${line} - 1`

#  	echo "    peers:" > temp.txt

  	for ((i=2;i<`expr ${COUNT_PEER_ADDCHANELL} + 2`;i++));do
  	string=${ARG_ARR[i]}
  	array=(${string//-/ })
    echo "      ${ARG_ARR[i]}: #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # [Optional]. will this peer be sent transaction proposals for endorsement? The peer must #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # have the chaincode installed. The app can also use this property to decide which peers #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # to send the chaincode install request. Default: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        endorsingPeer: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}

        # [Optional]. will this peer be sent query proposals? The peer must have the chaincode #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # installed. The app can also use this property to decide which peers to send the #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # chaincode install request. Default: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        chaincodeQuery: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}

        # [Optional]. will this peer be sent query proposals that do not require chaincodes, like #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # queryBlock(), queryTransaction(), etc. Default: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        ledgerQuery: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}

        # [Optional]. will this peer be the target of the SDK's listener registration? All peers can #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # produce events but the app typically only needs to connect to one to listen to events. #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        # Default: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}
        eventSource: true #TAGCHANNEL_${CHANNEL_NAME}_${array[1]}" >> temp.txt
  done
  sed -i "${line} r temp.txt" ${FILE_NAME}
  rm -rf temp.txt
}
peerJoinChannel

